package org.zebias.study.netty.boot;

import io.netty.channel.ChannelHandlerContext;
import org.zebias.study.netty.client.NettyRespClient;
import org.zebias.study.netty.codec.potobuf.SubscribeReqProto;

import java.util.Scanner;

/**
 * @ClassName AppClient
 * @Description TODO
 * @Author zebias
 * @Date 2020/8/18 11:55
 * @Version 1.0
 **/
public class AppClient {

    public static void main(String[] args) throws Exception {
        ;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new NettyRespClient().connect("127.0.0.1", 8088);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        sendMessage();
    }

    public static void sendMessage() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String context = scanner.next();
            SubscribeReqProto.SubscribeReq req = null;
            // 退出
            if ("exit".equals(context)) {
                System.exit(0);
            }
            if ("text".equals(context)) {
                SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
                builder.setSubReqID(2)
                        .setUserName("zebias")
                        .setProductName("text msg");
                req = builder.build();
            }
            if ("pic".equals(context)) {
                SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
                builder.setSubReqID(2)
                        .setUserName("zebias")
                        .setProductName("pic msg");
                req = builder.build();
            }
            if (ChannelStore.getInstance().getCtx("zebias") == null) {
                return;
            }
            ChannelStore.getInstance().getCtx("zebias").writeAndFlush(req);
        }
    }
}
